﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=News company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;
using CORE;

namespace KM.DAT
{
    /// <summary>
    /// This is datNews class
    /// </summary>

    public class datNews
    {
        public datNews()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa News===
        public void createEdit(entNews entN)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_NewsCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[15];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[1] = new SqlParameter("Name", DbType.Int32);
            sqla[2] = new SqlParameter("Tag", DbType.Int32);
            sqla[3] = new SqlParameter("Image", DbType.Int32);
            sqla[4] = new SqlParameter("Content", DbType.Int32);
            sqla[5] = new SqlParameter("Detail", DbType.Int32);
            sqla[6] = new SqlParameter("Date", DbType.Int32);
            sqla[7] = new SqlParameter("Title", DbType.Int32);
            sqla[8] = new SqlParameter("Description", DbType.Int32);
            sqla[9] = new SqlParameter("Keyword", DbType.Int32);
            sqla[10] = new SqlParameter("Priority", DbType.Int32);
            sqla[11] = new SqlParameter("Index", DbType.Int32);
            sqla[12] = new SqlParameter("Active", DbType.Int32);
            sqla[13] = new SqlParameter("GroupNewsId", DbType.Int32);
            sqla[14] = new SqlParameter("Lang", DbType.Int32);

            sqla[0].Value = entN.Id;
            sqla[1].Value = entN.Name;
            sqla[2].Value = entN.Tag;
            sqla[3].Value = entN.Image;
            sqla[4].Value = entN.Content;
            sqla[5].Value = entN.Detail;
            sqla[6].Value = entN.Date;
            sqla[7].Value = entN.Title;
            sqla[8].Value = entN.Description;
            sqla[9].Value = entN.Keyword;
            sqla[10].Value = entN.Priority;
            sqla[11].Value = entN.Index;
            sqla[12].Value = entN.Active;
            sqla[13].Value = entN.GroupNewsId;
            sqla[14].Value = entN.Lang;


            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall News===
        public List<entNews> getAll(int Id,ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_NewsGetAllByGroupNewsId", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("GroupNewsId", Id);
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entNews entN = new entNews();
            List<entNews> lst = new List<entNews>();
            while (dr.Read())
            {
                entN = new entNews();
                entN.Id = Convert.ToInt32(dr["Id"].ToString());
                entN.Name = dr["Name"].ToString();
                entN.Tag = dr["Tag"].ToString();
                entN.Image = dr["Image"].ToString();
                entN.Content = dr["Content"].ToString();
                entN.Detail = dr["Detail"].ToString();
                entN.Date = dr["Date"].ToString();
                entN.Title = dr["Title"].ToString();
                entN.Description = dr["Description"].ToString();
                entN.Keyword = dr["Keyword"].ToString();
                entN.Priority = Convert.ToInt32(dr["Priority"].ToString());
                entN.Index = Convert.ToInt32(dr["Index"].ToString());
                entN.Lang = dr["Lang"].ToString();

                lst.Add(entN);
            }
            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }
            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall News by id top===
        public List<entNews> getAll(int Id, int Top)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_NewsGetAllByGroupNewsIdAndTop", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("GroupNewsId", Id);
            cmd.Parameters.AddWithValue("@TOP", Top);
            dr = cmd.ExecuteReader();
            entNews entN = new entNews();
            List<entNews> lst = new List<entNews>();
            while (dr.Read())
            {
                entN = new entNews();
                entN.Id = Convert.ToInt32(dr["Id"].ToString());
                entN.Name = dr["Name"].ToString();
                entN.Tag = dr["Tag"].ToString();
                entN.Image = dr["Image"].ToString();
                entN.Content = dr["Content"].ToString();
                entN.Detail = dr["Detail"].ToString();
                entN.Date = dr["Date"].ToString();
                entN.Title = dr["Title"].ToString();
                entN.Description = dr["Description"].ToString();
                entN.Keyword = dr["Keyword"].ToString();
                entN.Priority = Convert.ToInt32(dr["Priority"].ToString());
                entN.Index = Convert.ToInt32(dr["Index"].ToString());
                entN.Lang = dr["Lang"].ToString();
                entN.Url = dr["Url"].ToString();

                lst.Add(entN);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall News by id top===
        public List<entNews> getAll(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_NewsGetAllByGroupNewsIdAndTop", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("GroupNewsId", Id);
            dr = cmd.ExecuteReader();
            entNews entN = new entNews();
            List<entNews> lst = new List<entNews>();
            while (dr.Read())
            {
                entN = new entNews();
                entN.Id = Convert.ToInt32(dr["Id"].ToString());
                entN.Name = dr["Name"].ToString();
                entN.Tag = dr["Tag"].ToString();
                entN.Image = dr["Image"].ToString();
                entN.Content = dr["Content"].ToString();
                entN.Detail = dr["Detail"].ToString();
                entN.Date = dr["Date"].ToString();
                entN.Title = dr["Title"].ToString();
                entN.Description = dr["Description"].ToString();
                entN.Keyword = dr["Keyword"].ToString();
                entN.Priority = Convert.ToInt32(dr["Priority"].ToString());
                entN.Index = Convert.ToInt32(dr["Index"].ToString());
                entN.Lang = dr["Lang"].ToString();
                entN.Url = dr["Url"].ToString();

                lst.Add(entN);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo News===
        public entNews getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_NewsGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entNews entN = new entNews();
            if (dr.Read())
            {
                entN.Id = Convert.ToInt32(dr["Id"].ToString());
                entN.Name = dr["Name"].ToString();
                entN.Tag = dr["Tag"].ToString();
                entN.Image = dr["Image"].ToString();
                entN.Content = dr["Content"].ToString();
                entN.Detail = dr["Detail"].ToString();
                entN.Date = dr["Date"].ToString();
                entN.Title = dr["Title"].ToString();
                entN.Description = dr["Description"].ToString();
                entN.Keyword = dr["Keyword"].ToString();
                entN.Priority = Convert.ToInt32(dr["Priority"].ToString());
                entN.Index = Convert.ToInt32(dr["Index"].ToString());
                entN.Active = Convert.ToInt32(dr["Active"].ToString());
                entN.GroupNewsId = Convert.ToInt32(dr["GroupNewsId"].ToString());
                entN.Lang = dr["Lang"].ToString();

            }
            dr.Close();
            sqlConnection.Close();
            return entN;
        }
        #endregion

        #region === xoa News===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_NewsDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
